﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ReNameEx.Model
{
    public class DeleteRule : ReExRuleBase
    {
        public override string GetDesc()
        {
            StringBuilder sb = new StringBuilder();
            string modeDesc = "";
            switch (SearchMode)
            {
                case 0:
                    modeDesc = "全部文本";
                    break;
                case 1:
                    modeDesc = "文本匹配";
                    break;
                case 2:
                    modeDesc = "正则匹配";
                    break;
                case 3:
                    modeDesc = "分隔符";
                    break;
            }
            sb.Append($"删除{modeDesc}");
            if (SearchMode > 0)
            {
                string countDesc = SearchCount == 0 ? "所有匹配项" : $"前 {SearchCount} 个匹配项";
                sb.Append($" '{SearchText}'，{countDesc}");
            }
            return sb.ToString();
        }


        public override void PrewRule(IEnumerable<ReExFile> files)
        {
            if (Enable)
            {
                foreach (ReExFile file in files)
                {
                    if (file.IsApply)
                    {
                        switch (SearchMode)
                        {
                            case 0:
                                file.NewFileName = "";
                                break;
                            case 1:
                                file.NewFileName = file.NewFileName.Replace(SearchText, "");
                                break;
                            case 2:
                                // 创建一个 Regex 对象
                                Regex regex = new Regex(SearchText);
                                // 使用 Replace 方法进行替换
                                if (SearchCount == 0)
                                    file.NewFileName = regex.Replace(file.NewFileName, "");
                                else
                                    file.NewFileName = regex.Replace(file.NewFileName, "", SearchCount);
                                break;
                            case 3:
                                var temparr = file.NewFileName.Split(SearchText);
                                if (temparr.Length > 1)
                                {
                                    int count = SearchCount == 0 ? temparr.Length : SearchCount;
                                    var tempdata = temparr.Skip(count);
                                    file.NewFileName = string.Join(SearchText, tempdata);
                                }
                                break;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 删除模式 0全部文本 1文本匹配 2正则匹配 3分隔符
        /// </summary>
        public int SearchMode { get; set; } = 0;

        /// <summary>
        /// 匹配文本
        /// </summary>
        public string SearchText { get; set; } = string.Empty;

        /// <summary>
        /// 匹配次数
        /// </summary>
        public int SearchCount { get; set; }

    }
}
